<!DOCTYPE html>
<html lang=zh>
<head><meta name="generator" content="Hexo 3.8.0">
  <meta charset="utf-8">
  
  <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
  <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, minimum-scale=1, user-scalable=no, minimal-ui">
  <meta name="renderer" content="webkit">
  <meta http-equiv="Cache-Control" content="no-transform">
  <meta http-equiv="Cache-Control" content="no-siteapp">
  <meta name="apple-mobile-web-app-capable" content="yes">
  <meta name="apple-mobile-web-app-status-bar-style" content="black">
  <meta name="format-detection" content="telephone=no,email=no,adress=no">
  <!-- Color theme for statusbar -->
  <meta name="theme-color" content="#000000">
  <!-- 强制页面在当前窗口以独立页面显示,防止别人在框架里调用页面 -->
  <meta http-equiv="window-target" content="_top">
  
  
  <title>C++11标准常用特性 | 王竹兴 | Blob</title>
  <meta name="description" content="概述 ​	C0x是C最新标准标准化过程中的曾用名，在这一系列文章中我们将介绍最新标准添加的一系列新的语言特性。在2011年9月份，C0x正式由官方发布并命名C11，现在很多编译器已经支持了部分C++11特性。 ​	C++11包括大量的新特性：主要特征像lambda表达式和移动语义，实用的类型推导关键字auto，更简单的容器遍历方法，和大量使模板更容易使用的改进。  可以使用大括号方式初始化列表，用">
<meta name="keywords" content="c&#x2F;c++">
<meta property="og:type" content="article">
<meta property="og:title" content="C++11标准常用特性">
<meta property="og:url" content="http://zhulao.gitee.io/blog/2019/02/21/C++11标准常用特性/index.html">
<meta property="og:site_name" content="王竹兴 | Blog">
<meta property="og:description" content="概述 ​	C0x是C最新标准标准化过程中的曾用名，在这一系列文章中我们将介绍最新标准添加的一系列新的语言特性。在2011年9月份，C0x正式由官方发布并命名C11，现在很多编译器已经支持了部分C++11特性。 ​	C++11包括大量的新特性：主要特征像lambda表达式和移动语义，实用的类型推导关键字auto，更简单的容器遍历方法，和大量使模板更容易使用的改进。  可以使用大括号方式初始化列表，用">
<meta property="og:locale" content="zh-CN">
<meta property="og:updated_time" content="2019-04-27T14:51:02.911Z">
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="C++11标准常用特性">
<meta name="twitter:description" content="概述 ​	C0x是C最新标准标准化过程中的曾用名，在这一系列文章中我们将介绍最新标准添加的一系列新的语言特性。在2011年9月份，C0x正式由官方发布并命名C11，现在很多编译器已经支持了部分C++11特性。 ​	C++11包括大量的新特性：主要特征像lambda表达式和移动语义，实用的类型推导关键字auto，更简单的容器遍历方法，和大量使模板更容易使用的改进。  可以使用大括号方式初始化列表，用">
  <!-- Canonical links -->
  <link rel="canonical" href="http://zhulao.gitee.io/blog/2019/02/21/C++11标准常用特性/index.html">
  
    <link rel="alternate" href="/atom.xml" title="王竹兴 | Blog" type="application/atom+xml">
  
  
    <link rel="icon" href="/favicon.png" type="image/x-icon">
  
  <link rel="stylesheet" href="/blog/css/style.css">
  
  
  
  
</head>


<body class="main-center" itemscope itemtype="http://schema.org/WebPage">
  <header class="header" itemscope itemtype="http://schema.org/WPHeader">
  <div class="slimContent">
    <div class="navbar-header">
      
      
      <div class="profile-block text-center">
        <a id="avatar" href="https://github.com/wzx3" target="_blank">
          <img class="img-circle img-rotate" src="/blog/images/avatar.jpeg" width="200" height="200">
        </a>
        <h2 id="name" class="hidden-xs hidden-sm">王竹兴</h2>
        <h3 id="title" class="hidden-xs hidden-sm hidden-md"></h3>
        <small id="location" class="text-muted hidden-xs hidden-sm"><i class="icon icon-map-marker"></i> XiaMen, China</small>
      </div>
      
      <div class="search" id="search-form-wrap">

    <form class="search-form sidebar-form">
        <div class="input-group">
            <input type="text" class="search-form-input form-control" placeholder="搜索">
            <span class="input-group-btn">
                <button type="submit" class="search-form-submit btn btn-flat" onclick="return false;"><i class="icon icon-search"></i></button>
            </span>
        </div>
    </form>
    <div class="ins-search">
  <div class="ins-search-mask"></div>
  <div class="ins-search-container">
    <div class="ins-input-wrapper">
      <input type="text" class="ins-search-input" placeholder="想要查找什么..." x-webkit-speech>
      <button type="button" class="close ins-close ins-selectable" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">×</span></button>
    </div>
    <div class="ins-section-wrapper">
      <div class="ins-section-container"></div>
    </div>
  </div>
</div>


</div>
      <button class="navbar-toggle collapsed" type="button" data-toggle="collapse" data-target="#main-navbar" aria-controls="main-navbar" aria-expanded="false">
        <span class="sr-only">Toggle navigation</span>
        <span class="icon-bar"></span>
        <span class="icon-bar"></span>
        <span class="icon-bar"></span>
      </button>
    </div>
    <nav id="main-navbar" class="collapse navbar-collapse" itemscope itemtype="http://schema.org/SiteNavigationElement" role="navigation">
      <ul class="nav navbar-nav main-nav menu-highlight">
        
        
        <li class="menu-item menu-item-home">
          <a href="/blog/.">
            
            <i class="icon icon-home-fill"></i>
            
            <span class="menu-title">首页</span>
          </a>
        </li>
        
        
        <li class="menu-item menu-item-archives">
          <a href="/blog/archives">
            
            <i class="icon icon-archives-fill"></i>
            
            <span class="menu-title">归档</span>
          </a>
        </li>
        
        
        <li class="menu-item menu-item-categories">
          <a href="/blog/categories">
            
            <i class="icon icon-folder"></i>
            
            <span class="menu-title">分类</span>
          </a>
        </li>
        
        
        <li class="menu-item menu-item-tags">
          <a href="/blog/tags">
            
            <i class="icon icon-tags"></i>
            
            <span class="menu-title">标签</span>
          </a>
        </li>
        
        
        <li class="menu-item menu-item-repository">
          <a href="/blog/repository">
            
            <i class="icon icon-project"></i>
            
            <span class="menu-title">项目</span>
          </a>
        </li>
        
        
        <li class="menu-item menu-item-about">
          <a href="/blog/about">
            
            <i class="icon icon-cup-fill"></i>
            
            <span class="menu-title">关于</span>
          </a>
        </li>
        
      </ul>
      
	
    <ul class="social-links">
    	
        <li><a href="https://github.com/wzx3" target="_blank" title="Github" data-toggle="tooltip" data-placement="top"><i class="icon icon-github"></i></a></li>
        
        <li><a href="/blog/atom.xml" target="_blank" title="Rss" data-toggle="tooltip" data-placement="top"><i class="icon icon-rss"></i></a></li>
        
    </ul>

    </nav>
  </div>
</header>

  
    <aside class="sidebar" itemscope itemtype="http://schema.org/WPSideBar">
  <div class="slimContent">
    
      <div class="widget">
    <h3 class="widget-title">公告</h3>
    <div class="widget-body">
        <div id="board">
            <div class="content">
                <p>人生最快意的事情之一, 就是你比别人牛逼, 然后静静的看别人在你面前装逼。</p>
            </div>
        </div>
    </div>
</div>

    
      
  <div class="widget">
    <h3 class="widget-title">分类</h3>
    <div class="widget-body">
      <ul class="category-list"><li class="category-list-item"><a class="category-list-link" href="/blog/categories/便签/">便签</a><span class="category-list-count">2</span></li><li class="category-list-item"><a class="category-list-link" href="/blog/categories/教程/">教程</a><span class="category-list-count">22</span></li><li class="category-list-item"><a class="category-list-link" href="/blog/categories/游记/">游记</a><span class="category-list-count">1</span></li><li class="category-list-item"><a class="category-list-link" href="/blog/categories/随笔/">随笔</a><span class="category-list-count">6</span></li></ul>
    </div>
  </div>


    
      
  <div class="widget">
    <h3 class="widget-title">标签云</h3>
    <div class="widget-body tagcloud">
      <a href="/blog/tags/DL/" style="font-size: 13.25px;">DL</a> <a href="/blog/tags/Debug/" style="font-size: 13.5px;">Debug</a> <a href="/blog/tags/Doc/" style="font-size: 13px;">Doc</a> <a href="/blog/tags/Hexo/" style="font-size: 13.25px;">Hexo</a> <a href="/blog/tags/Links/" style="font-size: 13px;">Links</a> <a href="/blog/tags/Linux/" style="font-size: 13.25px;">Linux</a> <a href="/blog/tags/Mac/" style="font-size: 13px;">Mac</a> <a href="/blog/tags/bazel/" style="font-size: 13px;">bazel</a> <a href="/blog/tags/c-c/" style="font-size: 13.75px;">c/c++</a> <a href="/blog/tags/git/" style="font-size: 13px;">git</a> <a href="/blog/tags/rst/" style="font-size: 13px;">rst</a> <a href="/blog/tags/scripts/" style="font-size: 13.75px;">scripts</a> <a href="/blog/tags/ssh/" style="font-size: 13px;">ssh</a> <a href="/blog/tags/国学经典/" style="font-size: 13px;">国学经典</a> <a href="/blog/tags/持续集成/" style="font-size: 13.25px;">持续集成</a> <a href="/blog/tags/科幻小说/" style="font-size: 14px;">科幻小说</a>
    </div>
  </div>

    
      
  <div class="widget">
    <h3 class="widget-title">归档</h3>
    <div class="widget-body">
      <ul class="archive-list"><li class="archive-list-item"><a class="archive-list-link" href="/blog/archives/2019/05/">五月 2019</a><span class="archive-list-count">6</span></li><li class="archive-list-item"><a class="archive-list-link" href="/blog/archives/2019/04/">四月 2019</a><span class="archive-list-count">10</span></li><li class="archive-list-item"><a class="archive-list-link" href="/blog/archives/2019/03/">三月 2019</a><span class="archive-list-count">6</span></li><li class="archive-list-item"><a class="archive-list-link" href="/blog/archives/2019/02/">二月 2019</a><span class="archive-list-count">10</span></li></ul>
    </div>
  </div>


    
      
  <div class="widget">
    <h3 class="widget-title">最新文章</h3>
    <div class="widget-body">
      <ul class="recent-post-list list-unstyled no-thumbnail">
        
          <li>
            
            <div class="item-inner">
              <p class="item-category">
                <a class="category-link" href="/blog/categories/随笔/">随笔</a>
              </p>
              <p class="item-title">
                <a href="/blog/2019/05/26/读论语/" class="title">读论语</a>
              </p>
              <p class="item-date">
                <time datetime="2019-05-26T01:45:07.000Z" itemprop="datePublished">2019-05-26</time>
              </p>
            </div>
          </li>
          
          <li>
            
            <div class="item-inner">
              <p class="item-category">
                <a class="category-link" href="/blog/categories/随笔/">随笔</a>
              </p>
              <p class="item-title">
                <a href="/blog/2019/05/20/读变型战争/" class="title">读变型战争</a>
              </p>
              <p class="item-date">
                <time datetime="2019-05-20T15:51:57.000Z" itemprop="datePublished">2019-05-20</time>
              </p>
            </div>
          </li>
          
          <li>
            
            <div class="item-inner">
              <p class="item-category">
                <a class="category-link" href="/blog/categories/随笔/">随笔</a>
              </p>
              <p class="item-title">
                <a href="/blog/2019/05/20/读星际远征/" class="title">读黑客横行</a>
              </p>
              <p class="item-date">
                <time datetime="2019-05-20T15:40:09.000Z" itemprop="datePublished">2019-05-20</time>
              </p>
            </div>
          </li>
          
          <li>
            
            <div class="item-inner">
              <p class="item-category">
                <a class="category-link" href="/blog/categories/教程/">教程</a>
              </p>
              <p class="item-title">
                <a href="/blog/2019/05/15/代码格式化工具-clang-format/" class="title">代码格式化工具:clang-format</a>
              </p>
              <p class="item-date">
                <time datetime="2019-05-15T14:39:51.000Z" itemprop="datePublished">2019-05-15</time>
              </p>
            </div>
          </li>
          
          <li>
            
            <div class="item-inner">
              <p class="item-category">
                <a class="category-link" href="/blog/categories/教程/">教程</a>
              </p>
              <p class="item-title">
                <a href="/blog/2019/05/12/Linux-疑难杂症/" class="title">Linux 疑难杂症</a>
              </p>
              <p class="item-date">
                <time datetime="2019-05-12T02:50:29.000Z" itemprop="datePublished">2019-05-12</time>
              </p>
            </div>
          </li>
          
      </ul>
    </div>
  </div>
  

    
  </div>
</aside>

  
  
<aside class="sidebar sidebar-toc collapse" id="collapseToc" itemscope itemtype="http://schema.org/WPSideBar">
  <div class="slimContent">
    <nav id="toc" class="article-toc">
      <h3 class="toc-title">文章目录</h3>
      <ol class="toc"><li class="toc-item toc-level-2"><a class="toc-link" href="#可以使用大括号方式初始化列表用在所有类上包括像vector这样的标准容器"><span class="toc-number">1.</span> <span class="toc-text"> 可以使用大括号方式初始化列表，用在所有类上，包括像vector这样的标准容器</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#使用-stdmove-移动构造避免大量的内存分配和内存拷贝操作"><span class="toc-number">2.</span> <span class="toc-text"> 使用 std::move 移动构造，避免大量的内存分配和内存拷贝操作</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#使用-auto-关键字来在运行时自动确定变量类型"><span class="toc-number">3.</span> <span class="toc-text"> 使用 auto 关键字，来在运行时自动确定变量类型</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#使用-decltype-关键字可以用来在编译期确定表达式的类型"><span class="toc-number">4.</span> <span class="toc-text"> 使用 decltype 关键字，可以用来在编译期确定表达式的类型</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#使用-for-for-each-for_each-基于范围的循环遍历"><span class="toc-number">5.</span> <span class="toc-text"> 使用 for、for each、for_each 基于范围的循环遍历</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#提供了匿名函数-lambda-函数"><span class="toc-number">6.</span> <span class="toc-text"> 提供了匿名函数( Lambda 函数)</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#提供-stdfunction-多态函数对象包容器"><span class="toc-number">7.</span> <span class="toc-text"> 提供 std::function 多态函数对象包容器</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#改进对象构造如类的构造可以调用该类的其他构造-类成员可以在类声明里面定义并初始化等"><span class="toc-number">8.</span> <span class="toc-text"> 改进对象构造，如类的构造可以调用该类的其他构造、类成员可以在类声明里面定义并初始化等</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#使用-override-标识符用于标识子类对父类中虚函数的重写"><span class="toc-number">9.</span> <span class="toc-text"> 使用 override 标识符，用于标识子类对父类中虚函数的重写</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#使用-final-标识符可让基类不被继承可让基类的虚函数不被派生类重写"><span class="toc-number">10.</span> <span class="toc-text"> 使用 final 标识符，可让基类不被继承，可让基类的虚函数不被派生类重写</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#使用-nullptr-关键字用作空指针"><span class="toc-number">11.</span> <span class="toc-text"> 使用 nullptr 关键字，用作空指针</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#使用-enum-class-做强类型枚举"><span class="toc-number">12.</span> <span class="toc-text"> 使用 enum class 做强类型枚举</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#使用-static_assert-关键字在编译期测试断言"><span class="toc-number">13.</span> <span class="toc-text"> 使用 static_assert 关键字，在编译期测试断言</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#sizeof-可以直接用于获取-classmember-的大小而不用通过-class的实例"><span class="toc-number">14.</span> <span class="toc-text"> sizeof 可以直接用于获取 Class::member 的大小，而不用通过 Class的实例</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#可以通过-default-强制编译器生成一个默认-constructor"><span class="toc-number">15.</span> <span class="toc-text"> 可以通过 =default 强制编译器生成一个默认 constructor</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#提供-shared_ptr-unique_ptr-weak_ptr-智能指针进行动态内存管理"><span class="toc-number">16.</span> <span class="toc-text"> 提供 shared_ptr、unique_ptr、weak_ptr 智能指针进行动态内存管理</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#四种类型转换符static_cast-dynamic_cast-reinterpret_cast-const_cast"><span class="toc-number">17.</span> <span class="toc-text"> 四种类型转换符：static_cast、dynamic_cast、reinterpret_cast、const_cast</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#提供线程类-stdthread-来运行一个新线程"><span class="toc-number">18.</span> <span class="toc-text"> 提供线程类 std::thread 来运行一个新线程</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#提供-stdchrono-库操作时间"><span class="toc-number">19.</span> <span class="toc-text"> 提供 std::chrono 库操作时间</span></a></li></ol>
    </nav>
  </div>
</aside>

<main class="main" role="main">
  <div class="content">
  <article id="post-C++11标准常用特性" class="article article-type-post" itemscope itemtype="http://schema.org/BlogPosting">
    
    <div class="article-header">
      
        
  
    <h1 class="article-title" itemprop="name">
      C++11标准常用特性
    </h1>
  

      
      <div class="article-meta">
        <span class="article-date">
    <i class="icon icon-calendar-check"></i>
	<a href="/blog/2019/02/21/C++11标准常用特性/" class="article-date">
	  <time datetime="2019-02-21T04:43:16.000Z" itemprop="datePublished">2019-02-21</time>
	</a>
</span>
        
  <span class="article-category">
    <i class="icon icon-folder"></i>
    <a class="article-category-link" href="/blog/categories/教程/">教程</a>
  </span>

        
  <span class="article-tag">
    <i class="icon icon-tags"></i>
	<a class="article-tag-link" href="/blog/tags/c-c/">c/c++</a>
  </span>


        
	<span class="article-read hidden-xs">
	    <i class="icon icon-eye-fill" aria-hidden="true"></i>
	    <span id="busuanzi_container_page_pv">
			<span id="busuanzi_value_page_pv">0</span>
		</span>
	</span>


        <span class="post-comment"><i class="icon icon-comment"></i> <a href="/blog/2019/02/21/C++11标准常用特性/#comments" class="article-comment-link">评论</a></span>
        
	
		<span class="post-wordcount hidden-xs" itemprop="wordCount">字数统计: 4,950(字)</span>
	
	
		<span class="post-readcount hidden-xs" itemprop="timeRequired">阅读时长: 25(分)</span>
	

      </div>
    </div>
    <div class="article-entry marked-body" itemprop="articleBody">
      
        <p><strong>概述</strong></p>
<p>​	C<ins>0x是C</ins>最新标准标准化过程中的曾用名，在这一系列文章中我们将介绍最新标准添加的一系列新的语言特性。在2011年9月份，C<ins>0x正式由官方发布并命名C</ins>11，现在很多编译器已经支持了部分C++11特性。</p>
<p>​	C++11包括大量的新特性：主要特征像<a href="http://en.cppreference.com/w/cpp/language/lambda" target="_blank" rel="noopener">lambda</a>表达式和移动语义，实用的类型推导关键字auto，更简单的容器遍历方法，和大量使模板更容易使用的改进。</p>
<h2 id="可以使用大括号方式初始化列表用在所有类上包括像vector这样的标准容器"><a class="markdownIt-Anchor" href="#可以使用大括号方式初始化列表用在所有类上包括像vector这样的标准容器"></a> 可以使用大括号方式初始化列表，用在所有类上，包括像vector这样的标准容器</h2>
<ul>
<li>容器初始化</li>
</ul>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">std::vector&lt;int&gt; l_vec = &#123; 2, 1, 3, 5, 4 &#125;;</span><br></pre></td></tr></table></figure>
<ul>
<li>类初始化 (有点行不通)</li>
</ul>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br></pre></td><td class="code"><pre><span class="line">#include &lt;iostream&gt;</span><br><span class="line">#include &lt;vector&gt;</span><br><span class="line"></span><br><span class="line">class DataClass</span><br><span class="line">&#123;</span><br><span class="line">public:</span><br><span class="line">	int m_iVal = 1;</span><br><span class="line">	double m_dVal = 6.;</span><br><span class="line">&#125;;</span><br><span class="line"></span><br><span class="line">int main()</span><br><span class="line">&#123;</span><br><span class="line">	std::vector&lt;DataClass&gt; l_vec = &#123; &#123;2, 66.&#125;, &#123;3, 666.&#125;, &#123;4, 6666.&#125; &#125;;</span><br><span class="line">	for (DataClass data : l_vec)</span><br><span class="line">	&#123;</span><br><span class="line">		std::cout &lt;&lt; data.m_iVal &lt;&lt; &quot;, &quot; &lt;&lt; data.m_dVal &lt;&lt; std::endl;</span><br><span class="line">	&#125;</span><br><span class="line"></span><br><span class="line">	return 0;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h2 id="使用-stdmove-移动构造避免大量的内存分配和内存拷贝操作"><a class="markdownIt-Anchor" href="#使用-stdmove-移动构造避免大量的内存分配和内存拷贝操作"></a> 使用 std::move 移动构造，避免大量的内存分配和内存拷贝操作</h2>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br></pre></td><td class="code"><pre><span class="line">#include &lt;list&gt;</span><br><span class="line"></span><br><span class="line">int main()</span><br><span class="line">&#123;</span><br><span class="line">	std::list&lt;int&gt; l_list = &#123; 1, 2, 3 &#125;;</span><br><span class="line"></span><br><span class="line">	&#123;</span><br><span class="line">		std::list&lt;int&gt; l_tmpList = std::move(l_list);</span><br><span class="line">		int l_tmp = 0;</span><br><span class="line">		for each (int var in l_tmpList)</span><br><span class="line">		&#123;</span><br><span class="line">			var++;</span><br><span class="line">		&#125;</span><br><span class="line"></span><br><span class="line">		for (int&amp; i : l_tmpList)</span><br><span class="line">		&#123;</span><br><span class="line">			i++;</span><br><span class="line">		&#125;</span><br><span class="line">	&#125;</span><br><span class="line"></span><br><span class="line">	return 0;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h2 id="使用-auto-关键字来在运行时自动确定变量类型"><a class="markdownIt-Anchor" href="#使用-auto-关键字来在运行时自动确定变量类型"></a> 使用 auto 关键字，来在运行时自动确定变量类型</h2>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line">#include &lt;vector&gt;</span><br><span class="line">#include &lt;iostream&gt;</span><br><span class="line"></span><br><span class="line">int main()</span><br><span class="line">&#123;</span><br><span class="line">	std::vector&lt;int&gt; l_vec = &#123; 1, 2, 3 &#125;;</span><br><span class="line">	for (auto l_itor = l_vec.begin(); l_itor != l_vec.end(); l_itor++)</span><br><span class="line">	&#123;</span><br><span class="line">		std::cout &lt;&lt; *l_itor;</span><br><span class="line">	&#125;</span><br><span class="line"></span><br><span class="line">    return 0;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h2 id="使用-decltype-关键字可以用来在编译期确定表达式的类型"><a class="markdownIt-Anchor" href="#使用-decltype-关键字可以用来在编译期确定表达式的类型"></a> 使用 decltype 关键字，可以用来在编译期确定表达式的类型</h2>
<h2 id="使用-for-for-each-for_each-基于范围的循环遍历"><a class="markdownIt-Anchor" href="#使用-for-for-each-for_each-基于范围的循环遍历"></a> 使用 for、for each、for_each 基于范围的循环遍历</h2>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br></pre></td><td class="code"><pre><span class="line">#include &lt;vector&gt;</span><br><span class="line">#include &lt;string&gt;</span><br><span class="line">#include &lt;iostream&gt;</span><br><span class="line">#include &lt;algorithm&gt;</span><br><span class="line"></span><br><span class="line">int main()</span><br><span class="line">&#123;</span><br><span class="line">	//使用 for (T var : Vec) 遍历</span><br><span class="line">	std::vector&lt;std::string&gt; l_strVec = &#123; &quot;zhulao&quot;, &quot;kangkang&quot; &#125;;</span><br><span class="line">	for (std::string str : l_strVec)</span><br><span class="line">	&#123;</span><br><span class="line">		std::cout &lt;&lt; str &lt;&lt; std::endl;</span><br><span class="line">	&#125;</span><br><span class="line"></span><br><span class="line">	//使用 for (T&amp; var : Vec) 对容器批量修改</span><br><span class="line">	std::vector&lt;int&gt; l_vec = &#123; 1, 2, 3 &#125;;</span><br><span class="line">	for (int&amp; var : l_vec)</span><br><span class="line">	&#123;</span><br><span class="line">		var++;</span><br><span class="line">	&#125;</span><br><span class="line">	//使用 for each (T var in Vec) 遍历容器</span><br><span class="line">	for each (int var in l_vec)</span><br><span class="line">	&#123;</span><br><span class="line">		std::cout &lt;&lt; var &lt;&lt; std::endl;</span><br><span class="line">	&#125;	</span><br><span class="line"></span><br><span class="line">	//使用 for_each(Vec.begin(), Vec.end(), 函数对象/Lambda表达式)遍历容器</span><br><span class="line">	std::vector&lt;double&gt; l_dVec = &#123; 1.0, 2.0, 6.66 &#125;;</span><br><span class="line">	double l_total = 0;</span><br><span class="line">	for_each(l_dVec.begin(), l_dVec.end(), [&amp;l_total](double var)</span><br><span class="line">	&#123;</span><br><span class="line">		l_total += var;</span><br><span class="line">	&#125;);</span><br><span class="line">	std::cout &lt;&lt; &quot;l_total = &quot; &lt;&lt; l_total &lt;&lt; std::endl;</span><br><span class="line"></span><br><span class="line">    return 0;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h2 id="提供了匿名函数-lambda-函数"><a class="markdownIt-Anchor" href="#提供了匿名函数-lambda-函数"></a> 提供了匿名函数( Lambda 函数)</h2>
<ul>
<li>排序容器数据，原先的写法</li>
</ul>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br></pre></td><td class="code"><pre><span class="line">#include &lt;algorithm&gt;</span><br><span class="line">#include &lt;vector&gt;</span><br><span class="line">#include &lt;iostream&gt;</span><br><span class="line"></span><br><span class="line">static int Compare(const int val1, const int val2)</span><br><span class="line">&#123;</span><br><span class="line">	return (val1 &lt; val2);</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">int main()</span><br><span class="line">&#123;</span><br><span class="line">	std::vector&lt;int&gt; l_iVec = &#123; 4, 2, 5, 1, 3, 666, 223 &#125;;</span><br><span class="line">	sort(l_iVec.begin(), l_iVec.end(), Compare);</span><br><span class="line">	for (int var : l_iVec)</span><br><span class="line">	&#123;</span><br><span class="line">		std::cout &lt;&lt; var &lt;&lt; std::endl;</span><br><span class="line">	&#125;</span><br><span class="line"></span><br><span class="line">	return 0;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<ul>
<li>排序容器数据，Lambda表达式写法</li>
</ul>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br></pre></td><td class="code"><pre><span class="line">#include &lt;iostream&gt;</span><br><span class="line">#include &lt;vector&gt;</span><br><span class="line">#include &lt;algorithm&gt;</span><br><span class="line"></span><br><span class="line">int main()</span><br><span class="line">&#123;</span><br><span class="line">	std::vector&lt;int&gt; l_iVec = &#123; 4, 2, 5, 1, 3, 666, 223 &#125;;</span><br><span class="line">	sort(l_iVec.begin(), l_iVec.end(), [](const int val1, const int val2)</span><br><span class="line">	&#123;</span><br><span class="line">		return val1 &lt; val2;</span><br><span class="line">	&#125;);</span><br><span class="line">	for (int var : l_iVec)</span><br><span class="line">	&#123;</span><br><span class="line">		std::cout &lt;&lt; var &lt;&lt; std::endl;</span><br><span class="line">	&#125;</span><br><span class="line"></span><br><span class="line">	return 0;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<ul>
<li>Lambda 表达式捕获列表的几种写法</li>
</ul>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br></pre></td><td class="code"><pre><span class="line">#include &lt;iostream&gt;</span><br><span class="line">#include &lt;string&gt;</span><br><span class="line"></span><br><span class="line">int main()</span><br><span class="line">&#123;</span><br><span class="line">	&#123;</span><br><span class="line">		int l_iNum = 1;</span><br><span class="line">		double l_dVal = 1.11;</span><br><span class="line">		std::string l_str = &quot;Hello World 1&quot;;</span><br><span class="line"></span><br><span class="line">		//值传递方式传递父作用域变量</span><br><span class="line">		auto l_func1 = [l_iNum, l_str] &#123;</span><br><span class="line">			std::cout &lt;&lt; &quot;func1: &quot; &lt;&lt; l_iNum &lt;&lt; &quot;, &quot; &lt;&lt; l_str &lt;&lt; std::endl;</span><br><span class="line">		&#125;;</span><br><span class="line">		l_func1();</span><br><span class="line"></span><br><span class="line">		//值传递捕获所有父作用域变量</span><br><span class="line">		auto l_func2 = [=] &#123;</span><br><span class="line">			std::cout &lt;&lt; &quot;func2: &quot; &lt;&lt; l_iNum &lt;&lt; &quot;, &quot; &lt;&lt; l_dVal &lt;&lt; &quot;, &quot; &lt;&lt; l_str &lt;&lt; std::endl;</span><br><span class="line">		&#125;;</span><br><span class="line">		l_func2();</span><br><span class="line"></span><br><span class="line">		//引用传递捕获父作用域变量</span><br><span class="line">		auto l_func3 = [&amp;l_iNum, &amp;l_dVal] &#123;</span><br><span class="line">			l_iNum = 333;</span><br><span class="line">			l_dVal = 3.33;</span><br><span class="line">		&#125;;</span><br><span class="line">		l_func3();</span><br><span class="line">		std::cout &lt;&lt; &quot;func3: &quot; &lt;&lt; l_iNum &lt;&lt; &quot;, &quot; &lt;&lt; l_dVal &lt;&lt; std::endl;</span><br><span class="line"></span><br><span class="line">		//引用传递捕获所有父作用域变量</span><br><span class="line">		auto l_func4 = [&amp;] &#123;</span><br><span class="line">			l_iNum = 444;</span><br><span class="line">			l_dVal = 4.44;</span><br><span class="line">			l_str = &quot;Hello World 4&quot;;</span><br><span class="line">		&#125;;</span><br><span class="line">		l_func4();</span><br><span class="line">		std::cout &lt;&lt; &quot;func4: &quot; &lt;&lt; l_iNum &lt;&lt; &quot;, &quot; &lt;&lt; l_dVal &lt;&lt; &quot;, &quot; &lt;&lt; l_str &lt;&lt; std::endl;</span><br><span class="line"></span><br><span class="line">		//值传递某个父作用域变量，引用传递其它变量</span><br><span class="line">		auto l_func5 = [&amp;, l_iNum] &#123;</span><br><span class="line">			//l_iNum++;		//此处编译器报错</span><br><span class="line">			l_dVal = 5.55;</span><br><span class="line">			l_str = &quot;Hello World 5&quot;;</span><br><span class="line">			std::cout &lt;&lt; &quot;func5: &quot; &lt;&lt; l_iNum;</span><br><span class="line">		&#125;;</span><br><span class="line">		l_func5();</span><br><span class="line">		std::cout &lt;&lt; &quot;, &quot; &lt;&lt; l_dVal &lt;&lt; &quot;, &quot; &lt;&lt; l_str &lt;&lt; std::endl;</span><br><span class="line"></span><br><span class="line">		//引用传递某个父作用域变量，值传递其它变量</span><br><span class="line">		auto l_func6 = [=, &amp;l_str] &#123;</span><br><span class="line">			l_str = &quot;HelloWorld 6&quot;;</span><br><span class="line">			std::cout &lt;&lt; &quot;func6: &quot; &lt;&lt; l_iNum &lt;&lt; &quot;, &quot; &lt;&lt; l_dVal;</span><br><span class="line">		&#125;;</span><br><span class="line">		l_func6();</span><br><span class="line">		std::cout &lt;&lt; &quot;, &quot; &lt;&lt; l_str &lt;&lt; std::endl;</span><br><span class="line">	&#125;</span><br><span class="line"></span><br><span class="line">	return 0;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<blockquote>
<p>捕获列表不允许变量重复传递，如[=, l_iNum] 或 [&amp;, &amp;l_iNum]。</p>
</blockquote>
<ul>
<li>Lambda 表达式传this指针</li>
</ul>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br></pre></td><td class="code"><pre><span class="line">#include &lt;iostream&gt;</span><br><span class="line">#include &lt;vector&gt;</span><br><span class="line">#include &lt;algorithm&gt;</span><br><span class="line"></span><br><span class="line">class SomeClass</span><br><span class="line">&#123;</span><br><span class="line">public:</span><br><span class="line">	void doLambda()</span><br><span class="line">	&#123;</span><br><span class="line">		std::vector&lt;int&gt; l_vec = &#123; 1, 2, 3 &#125;;</span><br><span class="line">		for_each(l_vec.begin(), l_vec.end(), [this](const int val) &#123;</span><br><span class="line">			std::cout &lt;&lt; val * m_privateVal &lt;&lt; std::endl;</span><br><span class="line">		&#125;);</span><br><span class="line">	&#125;</span><br><span class="line"></span><br><span class="line">private:</span><br><span class="line">	int getiValue() const &#123; return m_privateVal; &#125;;</span><br><span class="line">	int m_privateVal = 1;</span><br><span class="line">&#125;;</span><br><span class="line"></span><br><span class="line">int main()</span><br><span class="line">&#123;</span><br><span class="line">	SomeClass l_someClass;</span><br><span class="line">	l_someClass.doLambda();</span><br><span class="line"></span><br><span class="line">	return 0;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h2 id="提供-stdfunction-多态函数对象包容器"><a class="markdownIt-Anchor" href="#提供-stdfunction-多态函数对象包容器"></a> 提供 std::function 多态函数对象包容器</h2>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br></pre></td><td class="code"><pre><span class="line">#include &lt;iostream&gt;</span><br><span class="line">#include &lt;functional&gt;</span><br><span class="line"></span><br><span class="line">int main()</span><br><span class="line">&#123;</span><br><span class="line">	std::function&lt;void(void)&gt; l_func1 = [] &#123; std::cout &lt;&lt; &quot;HelloWorld!&quot; &lt;&lt; std::endl; &#125;;</span><br><span class="line">	std::function&lt;int(void)&gt; l_func2 = [] &#123; return 666; &#125;;</span><br><span class="line">	std::function&lt;int(int)&gt; l_func3 = [](int index) &#123; return index*index; &#125;;</span><br><span class="line">	std::function&lt;void(int&amp;)&gt; l_func4 = [](int&amp; index) &#123; index = index*index; &#125;;</span><br><span class="line"></span><br><span class="line">	l_func1();</span><br><span class="line">	std::cout &lt;&lt; &quot;l_func2() = &quot; &lt;&lt; l_func2() &lt;&lt; std::endl;</span><br><span class="line"></span><br><span class="line">	int l_tmp = 2;</span><br><span class="line">	std::cout &lt;&lt; &quot;l_func3(l_tmp) = &quot; &lt;&lt; l_func3(l_tmp) &lt;&lt; std::endl;</span><br><span class="line">	std::cout &lt;&lt; &quot;l_tmp = &quot; &lt;&lt; l_tmp &lt;&lt; std::endl;</span><br><span class="line"></span><br><span class="line">	l_func4(l_tmp);</span><br><span class="line">	std::cout &lt;&lt; &quot;l_tmp = &quot; &lt;&lt; l_tmp &lt;&lt; std::endl;</span><br><span class="line"></span><br><span class="line">	return 0;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br></pre></td><td class="code"><pre><span class="line">#include &lt;iostream&gt;</span><br><span class="line">#include &lt;functional&gt;</span><br><span class="line"></span><br><span class="line">struct MessageStruct &#123;</span><br><span class="line">	MessageStruct(int num) : m_num(num) &#123;&#125;</span><br><span class="line">	void print_add(int i) const &#123; std::cout &lt;&lt; &quot;MessageStruct, &quot; &lt;&lt; m_num + i &lt;&lt; std::endl; &#125;</span><br><span class="line"></span><br><span class="line">	int m_num;</span><br><span class="line">&#125;;</span><br><span class="line"></span><br><span class="line">struct PrintNumStruct</span><br><span class="line">&#123;</span><br><span class="line">	void operator()(int i) const</span><br><span class="line">	&#123;</span><br><span class="line">		std::cout &lt;&lt; &quot;PrintNumStruct, i = &quot; &lt;&lt; i &lt;&lt; std::endl;</span><br><span class="line">	&#125;</span><br><span class="line">&#125;;</span><br><span class="line"></span><br><span class="line">void print_num(int i)</span><br><span class="line">&#123;</span><br><span class="line">	std::cout &lt;&lt; &quot;print_num, i = &quot; &lt;&lt; i &lt;&lt; std::endl;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">int main()</span><br><span class="line">&#123;</span><br><span class="line">	//保存自由函数</span><br><span class="line">	std::function&lt;void(int)&gt; l_func111 = print_num;</span><br><span class="line">	l_func111(111);</span><br><span class="line"></span><br><span class="line">	//保存 lambda 表达式</span><br><span class="line">	std::function&lt;void()&gt; l_func2222 = []() &#123; print_num(222); &#125;;</span><br><span class="line">	l_func2222();</span><br><span class="line"></span><br><span class="line">	//保存 std::bind 结果</span><br><span class="line">	std::function&lt;void()&gt; l_func333 = std::bind(print_num, 333);</span><br><span class="line">	l_func333();</span><br><span class="line"></span><br><span class="line">	//保存成员函数</span><br><span class="line">	std::function&lt;void(const MessageStruct&amp;, int)&gt; l_func444 = &amp;MessageStruct::print_add;</span><br><span class="line">	MessageStruct l_struct(444);</span><br><span class="line">	l_func444(l_struct, 222);</span><br><span class="line"></span><br><span class="line">	//保存成员函数和对象</span><br><span class="line">	using std::placeholders::_1;</span><br><span class="line">	std::function&lt;void(int)&gt; l_func555 = std::bind(&amp;MessageStruct::print_add, l_struct, _1);</span><br><span class="line">	l_func555(111);</span><br><span class="line"></span><br><span class="line">	//保存成员函数和对象指针</span><br><span class="line">	std::function&lt;void(int)&gt; l_func777 = std::bind(&amp;MessageStruct::print_add, &amp;l_struct, _1);</span><br><span class="line">	l_func777(333);</span><br><span class="line"></span><br><span class="line">	//保存函数对象</span><br><span class="line">	std::function&lt;void(int)&gt; l_func888 = PrintNumStruct();</span><br><span class="line">	l_func888(888);</span><br><span class="line"></span><br><span class="line">	return 0;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h2 id="改进对象构造如类的构造可以调用该类的其他构造-类成员可以在类声明里面定义并初始化等"><a class="markdownIt-Anchor" href="#改进对象构造如类的构造可以调用该类的其他构造-类成员可以在类声明里面定义并初始化等"></a> 改进对象构造，如类的构造可以调用该类的其他构造、类成员可以在类声明里面定义并初始化等</h2>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br></pre></td><td class="code"><pre><span class="line">#include &lt;iostream&gt;</span><br><span class="line">#include &lt;string&gt;</span><br><span class="line"></span><br><span class="line">class SomeType</span><br><span class="line">&#123;</span><br><span class="line">public:</span><br><span class="line">	SomeType(const int iNum, const double dNum, const std::string str) : m_iVal(iNum), m_dVal(dNum), m_sVal(str) &#123;&#125;</span><br><span class="line">	SomeType() : SomeType(666, 888.0, &quot;Hello&quot;) &#123;&#125;</span><br><span class="line">	SomeType(const int iNum) : SomeType(iNum, 888., &quot;Hello&quot;) &#123;&#125;</span><br><span class="line">	SomeType(const int iNum, const std::string str) : SomeType(iNum, 888., str) &#123;&#125;</span><br><span class="line"></span><br><span class="line">	int m_iVal;</span><br><span class="line">	double m_dVal;</span><br><span class="line">	std::string m_sVal;</span><br><span class="line">&#125;;</span><br><span class="line"></span><br><span class="line">int main()</span><br><span class="line">&#123;</span><br><span class="line">	SomeType l_someType;</span><br><span class="line">	std::cout &lt;&lt; l_someType.m_iVal &lt;&lt; std::endl;</span><br><span class="line"></span><br><span class="line">	return 0;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<blockquote>
<p>这种新语法可以让这个默认值在实现中来设置而不是在接口中设置。</p>
</blockquote>
<blockquote>
<p>对库代码的维护者而言,在接口中(头文件中)声明默认值,这个默认值被嵌入到了调用端;要改变这个默认值的话,调用端的代码都需要重新编译.</p>
</blockquote>
<blockquote>
<p>但委托构造可以在实现中(CPP文件中)来改变这个默认值,这样调用端的代码就不需要重新编译,只用重新编译这个库就可以了</p>
</blockquote>
<ul>
<li>一个构造函数想要委托另一个构造函数，那么被委托的构造函数应该包含较大数量的参数，初始化较多的成员变量。</li>
<li>在委托其他构造函数后，不能再进行成员列表初始化，而只能在函数体内进行初始化其他成员变量。</li>
<li>类的静态成员不可在类内初始化。</li>
</ul>
<h2 id="使用-override-标识符用于标识子类对父类中虚函数的重写"><a class="markdownIt-Anchor" href="#使用-override-标识符用于标识子类对父类中虚函数的重写"></a> 使用 override 标识符，用于标识子类对父类中虚函数的重写</h2>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br></pre></td><td class="code"><pre><span class="line">class BaseClass</span><br><span class="line">&#123;</span><br><span class="line">public:</span><br><span class="line">	BaseClass() &#123;&#125;</span><br><span class="line"></span><br><span class="line">	virtual int getValue() const &#123; return m_value; &#125;</span><br><span class="line"></span><br><span class="line">	int m_value = 5;</span><br><span class="line">&#125;;</span><br><span class="line"></span><br><span class="line">class DerivedClass : public BaseClass</span><br><span class="line">&#123;</span><br><span class="line">public:</span><br><span class="line">	using BaseClass::BaseClass;</span><br><span class="line">	//DerivedClass() : BaseClass() &#123;&#125;</span><br><span class="line"></span><br><span class="line">	virtual int getValue() const override &#123; return m_value + 5; &#125;   //如果基类没有该函数签名的虚函数，则编译器会报错</span><br><span class="line">&#125;;</span><br><span class="line"></span><br><span class="line">int main()</span><br><span class="line">&#123;</span><br><span class="line">	DerivedClass l_data;</span><br><span class="line">	int l_tmp = l_data.getValue();</span><br><span class="line"></span><br><span class="line">	int override = 5;   //override只是标识符，不是关键字</span><br><span class="line"></span><br><span class="line">	return 0;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h2 id="使用-final-标识符可让基类不被继承可让基类的虚函数不被派生类重写"><a class="markdownIt-Anchor" href="#使用-final-标识符可让基类不被继承可让基类的虚函数不被派生类重写"></a> 使用 final 标识符，可让基类不被继承，可让基类的虚函数不被派生类重写</h2>
<ul>
<li>使用 final 标识基类不可继承</li>
</ul>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line">class BaseClass1 final</span><br><span class="line">&#123;</span><br><span class="line">&#125;;</span><br><span class="line"></span><br><span class="line">class DerivedClass1 : public BaseClass1	//这个地方编译器会报错</span><br><span class="line">&#123;</span><br><span class="line">&#125;;</span><br></pre></td></tr></table></figure>
<ul>
<li>使用 final 标识虚函数不可重写</li>
</ul>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line">class BaseClass2</span><br><span class="line">&#123;</span><br><span class="line">public:</span><br><span class="line">	virtual void func() final &#123;&#125;	//final标识的函数只能是虚函数</span><br><span class="line">&#125;;</span><br><span class="line"></span><br><span class="line">class DerivedClass2 : public BaseClass2</span><br><span class="line">&#123;</span><br><span class="line">public:</span><br><span class="line">	void func() &#123;&#125;	//这个地方编译器会报错</span><br><span class="line">&#125;;</span><br></pre></td></tr></table></figure>
<h2 id="使用-nullptr-关键字用作空指针"><a class="markdownIt-Anchor" href="#使用-nullptr-关键字用作空指针"></a> 使用 nullptr 关键字，用作空指针</h2>
<ul>
<li>在 C 中， NULL被定义为：</li>
</ul>
<blockquote>
<p>define NULL    ((void *)0)</p>
</blockquote>
<ul>
<li>C++中之所以不完全兼容：重载机制</li>
</ul>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br></pre></td><td class="code"><pre><span class="line">#include &lt;iostream&gt;</span><br><span class="line"></span><br><span class="line">class SomeType</span><br><span class="line">&#123;</span><br><span class="line">public:</span><br><span class="line">	void func(char* ptr) &#123; std::cout &lt;&lt; &quot;func, char* ...&quot; &lt;&lt; std::endl; &#125;</span><br><span class="line">	void func(int num) &#123; std::cout &lt;&lt; &quot;func, int ...&quot; &lt;&lt; std::endl; &#125;</span><br><span class="line">&#125;;</span><br><span class="line"></span><br><span class="line">int main()</span><br><span class="line">&#123;</span><br><span class="line">	SomeType l_someType;</span><br><span class="line">	l_someType.func(NULL);</span><br><span class="line"></span><br><span class="line">	return 0;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<blockquote>
<p>本来想输入的是一个空指针，可是输出的却是 func, int …</p>
</blockquote>
<ul>
<li>nullptr 可以转换成任何指针类型和bool布尔类型，就是不能被转换为整数</li>
<li>NULL 是一个宏定义，而 nullptr 是一个关键字</li>
</ul>
<h2 id="使用-enum-class-做强类型枚举"><a class="markdownIt-Anchor" href="#使用-enum-class-做强类型枚举"></a> 使用 enum class 做强类型枚举</h2>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br></pre></td><td class="code"><pre><span class="line">#include &lt;iostream&gt;</span><br><span class="line"></span><br><span class="line">enum class EnumClass1</span><br><span class="line">&#123;</span><br><span class="line">	Val1,</span><br><span class="line">	Val2,</span><br><span class="line">	Val3 = 100,</span><br><span class="line">	Val4</span><br><span class="line">&#125;;</span><br><span class="line"></span><br><span class="line">enum class EnumClass2</span><br><span class="line">&#123;</span><br><span class="line">	Val1 = 666,</span><br><span class="line">	Val2,</span><br><span class="line">	Val3 = 100,</span><br><span class="line">	Val4</span><br><span class="line">&#125;;</span><br><span class="line"></span><br><span class="line">int main()</span><br><span class="line">&#123;</span><br><span class="line">	std::cout &lt;&lt; static_cast&lt;std::underlying_type&lt;EnumClass1&gt;::type&gt;(EnumClass1::Val2) &lt;&lt; std::endl;</span><br><span class="line">	std::cout &lt;&lt; static_cast&lt;std::underlying_type&lt;EnumClass2&gt;::type&gt;(EnumClass2::Val2) &lt;&lt; std::endl;</span><br><span class="line"></span><br><span class="line">	return 0;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">输出结果为：</span><br><span class="line">1</span><br><span class="line">667</span><br></pre></td></tr></table></figure>
<h2 id="使用-static_assert-关键字在编译期测试断言"><a class="markdownIt-Anchor" href="#使用-static_assert-关键字在编译期测试断言"></a> 使用 static_assert 关键字，在编译期测试断言</h2>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line">int main()</span><br><span class="line">&#123;</span><br><span class="line">	//确保代码仅在32位机上编译</span><br><span class="line">	static_assert(sizeof(void *) == 4, &quot;64-bit code generation is not supported.&quot;);</span><br><span class="line">	</span><br><span class="line">	return 0;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h2 id="sizeof-可以直接用于获取-classmember-的大小而不用通过-class的实例"><a class="markdownIt-Anchor" href="#sizeof-可以直接用于获取-classmember-的大小而不用通过-class的实例"></a> sizeof 可以直接用于获取 Class::member 的大小，而不用通过 Class的实例</h2>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></td><td class="code"><pre><span class="line">#include &lt;string&gt;</span><br><span class="line">#include &lt;list&gt;</span><br><span class="line"></span><br><span class="line">class SomeType</span><br><span class="line">&#123;</span><br><span class="line">public:</span><br><span class="line">	std::string m_str = &quot;HelloWorld&quot;;</span><br><span class="line">&#125;;</span><br><span class="line"></span><br><span class="line">int main()</span><br><span class="line">&#123;</span><br><span class="line">	int l_tmp = sizeof(SomeType::m_str);</span><br><span class="line"></span><br><span class="line">	return 0;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h2 id="可以通过-default-强制编译器生成一个默认-constructor"><a class="markdownIt-Anchor" href="#可以通过-default-强制编译器生成一个默认-constructor"></a> 可以通过 =default 强制编译器生成一个默认 constructor</h2>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br></pre></td><td class="code"><pre><span class="line">class SomeType</span><br><span class="line">&#123;</span><br><span class="line">public:</span><br><span class="line">	SomeType() = default;</span><br><span class="line">	SomeType(const int iNum, const double dNum) : m_iVal(iNum), m_dVal(dNum) &#123;&#125;</span><br><span class="line"></span><br><span class="line">	int m_iVal;</span><br><span class="line">	double m_dVal;</span><br><span class="line">&#125;;</span><br><span class="line"></span><br><span class="line">int main()</span><br><span class="line">&#123;</span><br><span class="line">	SomeType l_someType;	//如果不写 SomeType() = default; 会编译失败</span><br><span class="line"></span><br><span class="line">	return 0;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h2 id="提供-shared_ptr-unique_ptr-weak_ptr-智能指针进行动态内存管理"><a class="markdownIt-Anchor" href="#提供-shared_ptr-unique_ptr-weak_ptr-智能指针进行动态内存管理"></a> 提供 shared_ptr、unique_ptr、weak_ptr 智能指针进行动态内存管理</h2>
<ul>
<li>shared_ptr 允许多个指针指向同一个对象</li>
</ul>
<hr>
<blockquote>
<p>初始化智能指针</p>
</blockquote>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br></pre></td><td class="code"><pre><span class="line">#include &lt;vector&gt;</span><br><span class="line">#include &lt;memory&gt;</span><br><span class="line"></span><br><span class="line">class SomeType</span><br><span class="line">&#123;</span><br><span class="line">public:</span><br><span class="line">	SomeType() &#123;&#125;</span><br><span class="line">	SomeType(const int iVal, const double dVal) : m_iVal(iVal), m_dVal(dVal) &#123;&#125;</span><br><span class="line">	</span><br><span class="line">	int m_iVal = 1;</span><br><span class="line">	double m_dVal = 2.;</span><br><span class="line">&#125;;</span><br><span class="line"></span><br><span class="line">int main()</span><br><span class="line">&#123;</span><br><span class="line">	std::shared_ptr&lt;SomeType&gt; l_ptr1(new SomeType);</span><br><span class="line">	std::shared_ptr&lt;SomeType&gt; l_ptr2 = std::make_shared&lt;SomeType&gt;(3, 6.);</span><br><span class="line">	std::shared_ptr&lt;std::vector&lt;int&gt;&gt; l_ptr3 = std::make_shared&lt;std::vector&lt;int&gt;&gt;(1);</span><br><span class="line"></span><br><span class="line">	l_ptr2.reset(l_ptr1.get());</span><br><span class="line"></span><br><span class="line">	int l_tmp = l_ptr2-&gt;m_iVal;</span><br><span class="line"></span><br><span class="line">	return 0;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<blockquote>
<p>使用 直接new表达式分配对象， 然后再传递给 shared_ptr(调用其构造)，会各自单独的分配控制块</p>
</blockquote>
<blockquote>
<p>使用 make_shared<t> 方式创建，其内部仅分配一次内存，消除了 shared_ptr 构造的开销</t></p>
</blockquote>
<hr>
<blockquote>
<p>智能指针指向空的判断</p>
</blockquote>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line">#include &lt;memory&gt;</span><br><span class="line"></span><br><span class="line">int main()</span><br><span class="line">&#123;</span><br><span class="line">	std::shared_ptr&lt;int&gt; l_ptr;</span><br><span class="line">	if (l_ptr.get() == nullptr)</span><br><span class="line">	&#123;</span><br><span class="line">		printf(&quot;指向空的判断!&quot;);</span><br><span class="line">	&#125;</span><br><span class="line"></span><br><span class="line">	return 0;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<hr>
<ul>
<li>unique_ptr 独占所指向的对象</li>
</ul>
<blockquote>
<p>不可以对unique_ptr进行拷贝、赋值等操作</p>
</blockquote>
<blockquote>
<p>可以通过release函数在unique_ptr之间转移控制权</p>
</blockquote>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br></pre></td><td class="code"><pre><span class="line">#include &lt;memory&gt;</span><br><span class="line"></span><br><span class="line">class SomeType</span><br><span class="line">&#123;</span><br><span class="line">public:</span><br><span class="line">	SomeType() &#123;&#125;</span><br><span class="line">	SomeType(const int iVal, const double dVal) : m_iVal(iVal), m_dVal(dVal) &#123;&#125;</span><br><span class="line"></span><br><span class="line">	int m_iVal = 1;</span><br><span class="line">	double m_dVal = 2.;</span><br><span class="line">&#125;;</span><br><span class="line"></span><br><span class="line">int main()</span><br><span class="line">&#123;</span><br><span class="line">	std::unique_ptr&lt;SomeType&gt; l_ptr1(new SomeType(3, 4.));</span><br><span class="line">	std::unique_ptr&lt;SomeType&gt; l_ptr2 = std::make_unique&lt;SomeType&gt;();</span><br><span class="line"></span><br><span class="line">	l_ptr2.reset(l_ptr1.release());</span><br><span class="line">	if (l_ptr1.get() == nullptr)</span><br><span class="line">	&#123;</span><br><span class="line">		printf(&quot;&quot;);	//l_ptr1指向的内存会释放掉</span><br><span class="line">	&#125;</span><br><span class="line"></span><br><span class="line">	int l_tmp = l_ptr2-&gt;m_iVal;</span><br><span class="line"></span><br><span class="line">	return 0;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<hr>
<ul>
<li>weak_ptr 是shared_ptr的弱引用</li>
</ul>
<blockquote>
<p>weak_ptr一般和shared_ptr配合使用</p>
</blockquote>
<blockquote>
<p>可以指向shared_ptr所指向的对象，但是却不增加对象的引用计数</p>
</blockquote>
<blockquote>
<p>这样就有可能出现weak_ptr所指向的对象实际上已经被释放了的情况。因此，weak_ptr有一个lock函数，尝试取回一个指向对象的shared_ptr。</p>
</blockquote>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br></pre></td><td class="code"><pre><span class="line">#include &lt;iostream&gt;</span><br><span class="line">#include &lt;memory&gt;</span><br><span class="line"></span><br><span class="line">std::weak_ptr&lt;int&gt; g_ptr;</span><br><span class="line"></span><br><span class="line">void function()</span><br><span class="line">&#123;</span><br><span class="line">	if (std::shared_ptr&lt;int&gt;l_tmptr = g_ptr.lock())</span><br><span class="line">	&#123;</span><br><span class="line">		std::cout &lt;&lt; &quot;*l_tmptr = &quot; &lt;&lt; *l_tmptr &lt;&lt; std::endl;</span><br><span class="line">	&#125;</span><br><span class="line">	else</span><br><span class="line">	&#123;</span><br><span class="line">		int* l_p = l_tmptr.get();</span><br><span class="line">		std::cout &lt;&lt; &quot;*l_tmptr = &quot; &lt;&lt; l_p &lt;&lt; std::endl;</span><br><span class="line">	&#125;</span><br><span class="line"></span><br><span class="line">	if (g_ptr.expired())</span><br><span class="line">	&#123;</span><br><span class="line">		std::cout &lt;&lt; &quot;g_ptr is expired &quot; &lt;&lt; std::endl;</span><br><span class="line">	&#125;</span><br><span class="line">	else</span><br><span class="line">	&#123;</span><br><span class="line">		std::cout &lt;&lt; &quot;g_ptr is exist &quot; &lt;&lt; std::endl;</span><br><span class="line">	&#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">int main()</span><br><span class="line">&#123;</span><br><span class="line">	&#123;</span><br><span class="line">		std::shared_ptr&lt;int&gt; l_ptr = std::make_shared&lt;int&gt;(42);</span><br><span class="line">		g_ptr = l_ptr;</span><br><span class="line"></span><br><span class="line">		function();</span><br><span class="line">	&#125;</span><br><span class="line"></span><br><span class="line">	function();</span><br><span class="line">	return 0;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h2 id="四种类型转换符static_cast-dynamic_cast-reinterpret_cast-const_cast"><a class="markdownIt-Anchor" href="#四种类型转换符static_cast-dynamic_cast-reinterpret_cast-const_cast"></a> 四种类型转换符：static_cast、dynamic_cast、reinterpret_cast、const_cast</h2>
<ul>
<li>static_cast 将一个类型转换为另一个相关类型</li>
</ul>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br></pre></td><td class="code"><pre><span class="line">class BaseClass</span><br><span class="line">&#123;</span><br><span class="line">&#125;;</span><br><span class="line"></span><br><span class="line">class DerivedClass : public BaseClass</span><br><span class="line">&#123;</span><br><span class="line">&#125;;</span><br><span class="line"></span><br><span class="line">class OtherClass</span><br><span class="line">&#123;</span><br><span class="line">&#125;;</span><br><span class="line"></span><br><span class="line">int main()</span><br><span class="line">&#123;</span><br><span class="line">	//数据类型的转换</span><br><span class="line">	int l_iVal = static_cast&lt;int&gt;(&apos;a&apos;);</span><br><span class="line"></span><br><span class="line">	//空指针类型的转换</span><br><span class="line">	BaseClass* l_ptr = static_cast&lt;BaseClass*&gt;(nullptr);</span><br><span class="line">	//BaseClass* l_ptr = (BaseClass*)(nullptr);</span><br><span class="line"></span><br><span class="line">	//把任何表达式转为void类型</span><br><span class="line">	void* l_ptr2 = static_cast&lt;void*&gt;(l_ptr);</span><br><span class="line"></span><br><span class="line">	//基类指针指向派生类类型</span><br><span class="line">	BaseClass* l_pBaseClass = static_cast&lt;BaseClass*&gt;(new DerivedClass);		//正确 类型安全</span><br><span class="line">	BaseClass* l_pBaseClass = new DerivedClass;									//等同上一句</span><br><span class="line">	</span><br><span class="line">	//派生类类型指向基类类型</span><br><span class="line">	DerivedClass* l_pDerivedClass = static_cast&lt;DerivedClass*&gt;(l_pBaseClass);	//正确 但是不安全</span><br><span class="line">	DerivedClass* l_pDerivedClass = (DerivedClass*)(l_pBaseClass);				//正确 但是不安全</span><br><span class="line">	DerivedClass* l_pDerivedClass = l_pBaseClass;								//错误 编译器报错</span><br><span class="line"></span><br><span class="line">	//无关类类型指向另一个无关类型</span><br><span class="line">	OtherClass* l_pOtherClass = static_cast&lt;OtherClass*&gt;(new DerivedClass);		//错误 编译器报错</span><br><span class="line">	BaseClass* l_pBaseClass = new OtherClass;									//错误 编译器报错</span><br><span class="line"></span><br><span class="line">	return 0;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<blockquote>
<p>可以用于基本数据类型之间的转换，如把int转换成char</p>
</blockquote>
<blockquote>
<p>可以进行空指针类型的转换</p>
</blockquote>
<blockquote>
<p>能把任何表达式转为void*类型</p>
</blockquote>
<blockquote>
<p>static_cast 不能转换掉带 const、volitale 修饰符的数据类型或指针</p>
</blockquote>
<blockquote>
<p>static_cast 需要转换的两者有&quot;一定的关系&quot;</p>
</blockquote>
<hr>
<ul>
<li>dynamic_cast 在继承层次中进行转换</li>
</ul>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br></pre></td><td class="code"><pre><span class="line">class BaseClass</span><br><span class="line">&#123;</span><br><span class="line">public:</span><br><span class="line">	virtual void func() &#123;&#125;</span><br><span class="line">&#125;;</span><br><span class="line"></span><br><span class="line">class DerivedClassA : public BaseClass</span><br><span class="line">&#123;</span><br><span class="line">	virtual void func() &#123;&#125;</span><br><span class="line">&#125;;</span><br><span class="line"></span><br><span class="line">class DerivedClassB : public BaseClass</span><br><span class="line">&#123;</span><br><span class="line">	virtual void func() &#123;&#125;</span><br><span class="line">&#125;;</span><br><span class="line"></span><br><span class="line">class OtherClass</span><br><span class="line">&#123;</span><br><span class="line">&#125;;</span><br><span class="line"></span><br><span class="line">int main()</span><br><span class="line">&#123;</span><br><span class="line">	//基类指针指向派生类类型</span><br><span class="line">	BaseClass* l_pBaseClass1 = dynamic_cast&lt;BaseClass*&gt;(new DerivedClassA);			//正确 但是类型不安全</span><br><span class="line">	BaseClass* l_pBaseClass2 = new DerivedClassA;</span><br><span class="line"></span><br><span class="line">	//派生类类型指向基类类型</span><br><span class="line">	DerivedClassA* l_pDerivedClass1 = dynamic_cast&lt;DerivedClassA*&gt;(l_pBaseClass1);	//正确 类型安全</span><br><span class="line">	DerivedClassA* l_pDerivedClass2 = (DerivedClassA*)(l_pDerivedClass2);			//正确 但是没有类型检查</span><br><span class="line">	DerivedClassA* l_pDerivedClass4 = new BaseClass;								//错误 编译器报错</span><br><span class="line"></span><br><span class="line">	//其它派生类指向基类类型</span><br><span class="line">	DerivedClassB* l_pDerivedClassB = dynamic_cast&lt;DerivedClassB*&gt;(l_pBaseClass1);	//正确 指针指向 nullptr</span><br><span class="line"></span><br><span class="line">	//无关类类型指向另一个无关类型</span><br><span class="line">	OtherClass* l_pOtherClass = dynamic_cast&lt;OtherClass*&gt;(new DerivedClassA);		//错误 编译器报错</span><br><span class="line"></span><br><span class="line">	return 0;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<blockquote>
<p>安全的基类和子类之间转换</p>
</blockquote>
<blockquote>
<p>必须要有虚函数</p>
</blockquote>
<blockquote>
<p>相同基类不同子类之间的交叉转换,结果是 nullptr</p>
</blockquote>
<hr>
<ul>
<li>reinterpret_cast 将类型转换为无关的类型</li>
</ul>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br></pre></td><td class="code"><pre><span class="line">class BaseClass</span><br><span class="line">&#123;</span><br><span class="line">&#125;;</span><br><span class="line"></span><br><span class="line">class DerivedClass : public BaseClass</span><br><span class="line">&#123;</span><br><span class="line">&#125;;</span><br><span class="line"></span><br><span class="line">class OtherClass</span><br><span class="line">&#123;</span><br><span class="line">&#125;;</span><br><span class="line"></span><br><span class="line">int main()</span><br><span class="line">&#123;</span><br><span class="line">	//可以把一个指针转换成一个整数</span><br><span class="line">	int l_iVal = reinterpret_cast&lt;int&gt;(nullptr);</span><br><span class="line"></span><br><span class="line">	//可以把一个整数转换成一个指针</span><br><span class="line">	void* l_ptr = reinterpret_cast&lt;void*&gt;(l_iVal);</span><br><span class="line"></span><br><span class="line">	//能够在非相关的类型之间转换</span><br><span class="line">	OtherClass* l_pOtherClass = reinterpret_cast&lt;OtherClass*&gt;(new BaseClass);</span><br><span class="line">	//OtherClass* l_pOtherClass = (OtherClass*)(new BaseClass);</span><br><span class="line"></span><br><span class="line">	return 0;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<blockquote>
<p>可以把一个指针转换成一个整数</p>
</blockquote>
<blockquote>
<p>可以把一个整数转换成一个指针</p>
</blockquote>
<blockquote>
<p>能够在非相关的类型之间转换</p>
</blockquote>
<blockquote>
<p>不能转换掉带 const、volitale 修饰符的数据类型或指针</p>
</blockquote>
<blockquote>
<p>在类型之间指向的内容不做任何类型的检查和转换</p>
</blockquote>
<blockquote>
<p>平台移植性比较差</p>
</blockquote>
<hr>
<ul>
<li>const_cast 修改 const/volatile 限定符</li>
</ul>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></td><td class="code"><pre><span class="line">int main()</span><br><span class="line">&#123;</span><br><span class="line">	//常量指针被转化成非常量指针，并且仍然指向原来的对象</span><br><span class="line">	const int* l_iCPtr = new int(2333);</span><br><span class="line">	int* l_iPtr = const_cast&lt;int*&gt;(l_iCPtr);</span><br><span class="line">	*l_iPtr = 666;</span><br><span class="line">	delete l_iCPtr;</span><br><span class="line"></span><br><span class="line">	//常量引用被转换成非常量引用，并且仍然指向原来的对象</span><br><span class="line">	const int&amp; l_refCIVal = 666;</span><br><span class="line">	int&amp; l_refIVal = const_cast&lt;int&amp;&gt;(l_refCIVal);</span><br><span class="line">	l_refIVal = 888;</span><br><span class="line"></span><br><span class="line">	return 0;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<blockquote>
<p>常量指针被转化成非常量指针，并且仍然指向原来的对象</p>
</blockquote>
<blockquote>
<p>常量引用被转换成非常量引用，并且仍然指向原来的对象</p>
</blockquote>
<h2 id="提供线程类-stdthread-来运行一个新线程"><a class="markdownIt-Anchor" href="#提供线程类-stdthread-来运行一个新线程"></a> 提供线程类 std::thread 来运行一个新线程</h2>
<ul>
<li>一个 std::thread 对象可以接收</li>
</ul>
<ol>
<li>普通的函数</li>
<li>函数对象</li>
<li>类的成员函数</li>
<li>lambda 函数</li>
</ol>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br></pre></td><td class="code"><pre><span class="line">#include &lt;iostream&gt;</span><br><span class="line">#include &lt;string&gt;</span><br><span class="line">#include &lt;thread&gt;</span><br><span class="line">#include &lt;functional&gt;</span><br><span class="line"></span><br><span class="line">//无形参普通函数</span><br><span class="line">void func1()</span><br><span class="line">&#123;</span><br><span class="line">	std::cout &lt;&lt; &quot;this is func1.&quot; &lt;&lt; std::endl;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">//有形参普通函数</span><br><span class="line">void func2(const std::string&amp; str)</span><br><span class="line">&#123;</span><br><span class="line">	std::cout &lt;&lt; &quot;this is func2. str = &quot; &lt;&lt; str &lt;&lt; std::endl;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">//无形参类成员函数</span><br><span class="line">class Class3</span><br><span class="line">&#123;</span><br><span class="line">public:</span><br><span class="line">	void func3()</span><br><span class="line">	&#123;</span><br><span class="line">		std::cout &lt;&lt; &quot;this is func3.&quot; &lt;&lt; std::endl;</span><br><span class="line">	&#125;</span><br><span class="line">&#125;;</span><br><span class="line"></span><br><span class="line">//有形参类成员函数</span><br><span class="line">class Class4</span><br><span class="line">&#123;</span><br><span class="line">public:</span><br><span class="line">	void func4(const std::string&amp; str)</span><br><span class="line">	&#123;</span><br><span class="line">		std::cout &lt;&lt; &quot;this is func4. str = &quot; &lt;&lt; str &lt;&lt; std::endl;</span><br><span class="line">	&#125;</span><br><span class="line">&#125;;</span><br><span class="line"></span><br><span class="line">int main()</span><br><span class="line">&#123;</span><br><span class="line">	std::thread l_thr1(func1);</span><br><span class="line">	std::thread l_thr2(func2, &quot;Hello&quot;);</span><br><span class="line">	//std::thread l_thr2(std::bind(func2, &quot;Hello&quot;));</span><br><span class="line"></span><br><span class="line">	Class3 l_class3;</span><br><span class="line">	std::thread l_thr3(&amp;Class3::func3, &amp;l_class3);</span><br><span class="line"></span><br><span class="line">	Class4 l_class4;</span><br><span class="line">	std::thread l_thr4(&amp;Class4::func4, &amp;l_class4, &quot;World&quot;);</span><br><span class="line"></span><br><span class="line">	//函数对象</span><br><span class="line">	std::function&lt;void(std::string)&gt; l_func5 = [](std::string str) &#123; std::cout &lt;&lt; &quot;str = &quot; &lt;&lt; str &lt;&lt; std::endl; &#125;;</span><br><span class="line">	//std::function&lt;std::string(void)&gt; l_func2 = [] &#123; return &quot;Hello World!&quot;; &#125;;</span><br><span class="line">	std::thread l_thr5(l_func5, &quot;this is thread 5.&quot;);</span><br><span class="line"></span><br><span class="line">	//Lambda 表达式</span><br><span class="line">	int l_iNum = 666;</span><br><span class="line">	std::thread l_thr6([&amp;l_iNum](std::string str) &#123; std::cout &lt;&lt; &quot;str = &quot; &lt;&lt; str &lt;&lt; std::endl; &#125;, &quot;this is thread 6&quot;);</span><br><span class="line"></span><br><span class="line">	l_thr1.join();</span><br><span class="line">	l_thr2.join();</span><br><span class="line">	l_thr3.join();</span><br><span class="line">	l_thr4.join();</span><br><span class="line">	l_thr5.join();</span><br><span class="line">	l_thr6.join();</span><br><span class="line"></span><br><span class="line">	return 0;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<ul>
<li>多线程之互斥锁 售票Demo</li>
</ul>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br></pre></td><td class="code"><pre><span class="line">#include &lt;iostream&gt;</span><br><span class="line">#include &lt;thread&gt;</span><br><span class="line">#include &lt;mutex&gt;</span><br><span class="line">#include &lt;functional&gt;</span><br><span class="line">#include &lt;chrono&gt;</span><br><span class="line"></span><br><span class="line">class SellTicket</span><br><span class="line">&#123;</span><br><span class="line">public:</span><br><span class="line">	SellTicket() = default;</span><br><span class="line">	virtual ~SellTicket()</span><br><span class="line">	&#123;</span><br><span class="line">		m_thr1.join();</span><br><span class="line">		m_thr2.join();</span><br><span class="line">	&#125;</span><br><span class="line"></span><br><span class="line">	void start()</span><br><span class="line">	&#123;</span><br><span class="line">		m_thr1 = std::thread(m_sellTicketA);</span><br><span class="line">		m_thr2 = std::thread(m_sellTicketB);</span><br><span class="line">	&#125;</span><br><span class="line"></span><br><span class="line">private:</span><br><span class="line">	int m_tickets = 100;				//售票总数</span><br><span class="line"></span><br><span class="line">	std::thread m_thr1;</span><br><span class="line">	std::thread m_thr2;</span><br><span class="line"></span><br><span class="line">	std::mutex m_mutex;</span><br><span class="line"></span><br><span class="line">	//售票员A</span><br><span class="line">	std::function&lt;void(void)&gt; m_sellTicketA = [this] &#123;</span><br><span class="line">		while (true)</span><br><span class="line">		&#123;</span><br><span class="line">			std::unique_lock&lt;std::mutex&gt; l_lock(m_mutex);</span><br><span class="line">			if (m_tickets &gt; 0)</span><br><span class="line">			&#123;</span><br><span class="line">				std::this_thread::sleep_for(std::chrono::milliseconds(10));			//假设每10ms卖出一张票</span><br><span class="line">				std::cout &lt;&lt; &quot;A Sell the &quot; &lt;&lt; m_tickets-- &lt;&lt; &quot;th ticket.&quot; &lt;&lt; std::endl;</span><br><span class="line">			&#125;</span><br><span class="line">			else</span><br><span class="line">			&#123;</span><br><span class="line">				break;</span><br><span class="line">			&#125;</span><br><span class="line">		&#125;</span><br><span class="line">	&#125;;</span><br><span class="line">	//售票员B</span><br><span class="line">	std::function&lt;void(void)&gt; m_sellTicketB = [this] &#123;</span><br><span class="line">		while (true)</span><br><span class="line">		&#123;</span><br><span class="line">			std::unique_lock&lt;std::mutex&gt; l_lock(m_mutex);</span><br><span class="line">			if (m_tickets &gt; 0)</span><br><span class="line">			&#123;</span><br><span class="line">				std::this_thread::sleep_for(std::chrono::milliseconds(100));		//假设每100ms卖出一张票</span><br><span class="line">				std::cout &lt;&lt; &quot;B Sell the &quot; &lt;&lt; m_tickets-- &lt;&lt; &quot;th ticket.&quot; &lt;&lt; std::endl;</span><br><span class="line">			&#125;</span><br><span class="line">			else</span><br><span class="line">			&#123;</span><br><span class="line">				break;</span><br><span class="line">			&#125;</span><br><span class="line">		&#125;</span><br><span class="line">	&#125;;</span><br><span class="line">&#125;;</span><br><span class="line"></span><br><span class="line">int main()</span><br><span class="line">&#123;</span><br><span class="line">	SellTicket l_sellTicket;</span><br><span class="line">	l_sellTicket.start();</span><br><span class="line"></span><br><span class="line">	return 0;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<ul>
<li>多线程之条件变量 生产者与消费者Demo</li>
</ul>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br></pre></td><td class="code"><pre><span class="line">#include &lt;iostream&gt;</span><br><span class="line">#include &lt;thread&gt;</span><br><span class="line">#include &lt;mutex&gt;</span><br><span class="line">#include &lt;condition_variable&gt;</span><br><span class="line">#include &lt;functional&gt;</span><br><span class="line">#include &lt;chrono&gt;</span><br><span class="line"></span><br><span class="line">#define SELL_TICKETS_5TIMES 5</span><br><span class="line"></span><br><span class="line">int  g_tickets = 0;</span><br><span class="line"></span><br><span class="line">class SellTickets</span><br><span class="line">&#123;</span><br><span class="line">public:</span><br><span class="line">	SellTickets() = default;</span><br><span class="line">	virtual ~SellTickets()</span><br><span class="line">	&#123;	</span><br><span class="line">		m_thr1.join();</span><br><span class="line">		m_thr2.join();</span><br><span class="line">	&#125;</span><br><span class="line"></span><br><span class="line">	void start()</span><br><span class="line">	&#123;</span><br><span class="line">		m_thr1 = std::thread(m_sellTicket);</span><br><span class="line">		m_thr2 = std::thread(m_buyTicket);</span><br><span class="line">	&#125;</span><br><span class="line"></span><br><span class="line">protected:</span><br><span class="line">	//消费者</span><br><span class="line">	std::function&lt;void(void)&gt; m_buyTicket = [this] &#123;</span><br><span class="line">		while (true)</span><br><span class="line">		&#123;</span><br><span class="line">			std::unique_lock&lt;std::mutex&gt; l_lock(m_mutex);</span><br><span class="line">			while (!g_tickets)</span><br><span class="line">			&#123;</span><br><span class="line">				if (m_count == SELL_TICKETS_5TIMES)</span><br><span class="line">				&#123;</span><br><span class="line">					std::cout &lt;&lt; &quot;No Tickets! ending...&quot; &lt;&lt; std::endl;</span><br><span class="line">					return;</span><br><span class="line">				&#125;</span><br><span class="line">				std::cout &lt;&lt; &quot;No Tickets! wait...&quot; &lt;&lt; std::endl;</span><br><span class="line">				m_cond.wait(l_lock);</span><br><span class="line">			&#125;</span><br><span class="line"></span><br><span class="line">			std::this_thread::sleep_for(std::chrono::milliseconds(10));</span><br><span class="line">			std::cout &lt;&lt; &quot;Buy the &quot; &lt;&lt; g_tickets-- &lt;&lt; &quot;th ticket.&quot; &lt;&lt; std::endl;</span><br><span class="line">		&#125;</span><br><span class="line">	&#125;;</span><br><span class="line">	//生产者</span><br><span class="line">	std::function&lt;void(void)&gt; m_sellTicket = [this] &#123;</span><br><span class="line">		while (m_count &lt; SELL_TICKETS_5TIMES)</span><br><span class="line">		&#123;</span><br><span class="line">			std::this_thread::sleep_for(std::chrono::milliseconds(1000));</span><br><span class="line">			std::lock_guard&lt;std::mutex&gt; l_lock(m_mutex);</span><br><span class="line">			g_tickets = 10;</span><br><span class="line">			m_cond.notify_one();</span><br><span class="line">			std::cout &lt;&lt; &quot;Start Sell the &quot; &lt;&lt; ++m_count &lt;&lt; &quot;th time Tickets!&quot; &lt;&lt; std::endl;</span><br><span class="line">		&#125;</span><br><span class="line">	&#125;;</span><br><span class="line"></span><br><span class="line">private:</span><br><span class="line">	int m_count = 0;</span><br><span class="line"></span><br><span class="line">	std::thread m_thr1;</span><br><span class="line">	std::thread m_thr2;</span><br><span class="line">	std::mutex m_mutex;</span><br><span class="line">	std::condition_variable m_cond;	</span><br><span class="line">&#125;;</span><br><span class="line"></span><br><span class="line">int main()</span><br><span class="line">&#123;</span><br><span class="line">	SellTickets l_sellTickets;</span><br><span class="line">	l_sellTickets.start();</span><br><span class="line"></span><br><span class="line">	return 0;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h2 id="提供-stdchrono-库操作时间"><a class="markdownIt-Anchor" href="#提供-stdchrono-库操作时间"></a> 提供 std::chrono 库操作时间</h2>
<ul>
<li>获取某个函数用时多少毫米的 Demo</li>
</ul>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br></pre></td><td class="code"><pre><span class="line">#include &lt;iostream&gt;</span><br><span class="line">#include &lt;chrono&gt;</span><br><span class="line">#include &lt;ctime&gt;</span><br><span class="line"></span><br><span class="line">class TimeElapsed</span><br><span class="line">&#123;</span><br><span class="line">public:</span><br><span class="line">	TimeElapsed() = default;</span><br><span class="line"></span><br><span class="line">	void start()</span><br><span class="line">	&#123;</span><br><span class="line">		m_ptStart = std::chrono::system_clock::now();</span><br><span class="line">	&#125;</span><br><span class="line"></span><br><span class="line">	int elapsed()</span><br><span class="line">	&#123;</span><br><span class="line">		m_ptEnd = std::chrono::system_clock::now();</span><br><span class="line"></span><br><span class="line">		return std::chrono::duration_cast&lt;std::chrono::milliseconds&gt;(m_ptEnd - m_ptStart).count();</span><br><span class="line">	&#125;</span><br><span class="line"></span><br><span class="line">private:</span><br><span class="line">	std::chrono::system_clock::time_point m_ptStart;</span><br><span class="line">	std::chrono::system_clock::time_point m_ptEnd;</span><br><span class="line">&#125;;</span><br><span class="line"></span><br><span class="line">void func()</span><br><span class="line">&#123;</span><br><span class="line">	for (int i = 0; i &lt; 20000; i++)</span><br><span class="line">	&#123;</span><br><span class="line">		printf(&quot;&quot;);</span><br><span class="line">	&#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">int main()</span><br><span class="line">&#123;</span><br><span class="line">	TimeElapsed l_time;</span><br><span class="line">	l_time.start();</span><br><span class="line">	func();</span><br><span class="line">	std::cout &lt;&lt; &quot;elapsed &quot; &lt;&lt; l_time.elapsed() &lt;&lt; &quot; ms&quot;;</span><br><span class="line"></span><br><span class="line">	return 0;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

      
    </div>
    <div class="article-footer">
      <!-- <blockquote class="mt-2x">
  <ul class="post-copyright list-unstyled">
    
    <li class="post-copyright-link hidden-xs">
      <strong>本文链接：</strong>
      <a href="http://zhulao.gitee.io/blog/2019/02/21/C++11标准常用特性/" title="C++11标准常用特性" target="_blank" rel="external">http://zhulao.gitee.io/blog/2019/02/21/C++11标准常用特性/</a>
    </li>
    
    <li class="post-copyright-license">
      <strong>版权声明： </strong> 本博客所有文章除特别声明外，均采用 <a href="http://creativecommons.org/licenses/by/4.0/deed.zh" target="_blank" rel="external">CC BY 4.0 CN协议</a> 许可协议。转载请注明出处！
    </li>
  </ul>
</blockquote>


<div class="panel panel-default panel-badger">
  <div class="panel-body">
    <figure class="media">
      <div class="media-left">
        <a href="https://github.com/wzx3" target="_blank" class="img-burn thumb-sm visible-lg">
          <img src="/blog/images/avatar.jpeg" class="img-rounded w-full" alt="">
        </a>
      </div>
      <div class="media-body">
        <h3 class="media-heading"><a href="https://github.com/wzx3" target="_blank"><span class="text-dark">王竹兴</span><small class="ml-1x"></small></a></h3>
        <div></div>
      </div>
    </figure>
  </div>
</div>
 -->

    </div>
  </article>
  
    
  <section id="comments">
  	
  </section>


  
</div>

  <nav class="bar bar-footer clearfix" data-stick-bottom>
  <div class="bar-inner">
  
  <ul class="pager pull-left">
    
    <li class="prev">
      <a href="/blog/2019/02/21/Hexo表格Bug及C语言运算符优先级列表/" title="Hexo表格Bug及C语言运算符优先级列表"><i class="icon icon-angle-left" aria-hidden="true"></i><span>&nbsp;&nbsp;上一篇</span></a>
    </li>
    
    
    <li class="next">
      <a href="/blog/2019/02/20/2018-11-25厦门GDG谷歌开发者节/" title="2018-11-25厦门GDG谷歌开发者节"><span>下一篇&nbsp;&nbsp;</span><i class="icon icon-angle-right" aria-hidden="true"></i></a>
    </li>
    
    
    <li class="toggle-toc">
      <a class="toggle-btn collapsed" data-toggle="collapse" href="#collapseToc" aria-expanded="false" title="文章目录" role="button">
        <span>[&nbsp;</span><span>文章目录</span>
        <i class="text-collapsed icon icon-anchor"></i>
        <i class="text-in icon icon-close"></i>
        <span>]</span>
      </a>
    </li>
    
  </ul>
  
  
  
  <div class="bar-right">
    
    <div class="share-component" data-sites="weibo,qq,wechat,facebook,twitter" data-mobile-sites="weibo,qq,qzone"></div>
    
  </div>
  </div>
</nav>
  


</main>

  <footer class="footer" itemscope itemtype="http://schema.org/WPFooter">
	
	
    <ul class="social-links">
    	
        <li><a href="https://github.com/wzx3" target="_blank" title="Github" data-toggle="tooltip" data-placement="top"><i class="icon icon-github"></i></a></li>
        
        <li><a href="/blog/atom.xml" target="_blank" title="Rss" data-toggle="tooltip" data-placement="top"><i class="icon icon-rss"></i></a></li>
        
    </ul>

    <div class="copyright">
    	
        <div class="publishby">
        	Theme by <a href="https://github.com/cofess" target="_blank"> cofess </a>base on <a href="https://github.com/cofess/hexo-theme-pure" target="_blank">pure</a>.
        </div>
    </div>
</footer>
  <script src="//cdn.jsdelivr.net/npm/jquery@1.12.4/dist/jquery.min.js"></script>
<script>
window.jQuery || document.write('<script src="js/jquery.min.js"><\/script>')
</script>
<script src="/blog/js/plugin.min.js"></script>
<script src="/blog/js/application.js"></script>

    <script>
(function (window) {
    var INSIGHT_CONFIG = {
        TRANSLATION: {
            POSTS: '文章',
            PAGES: '页面',
            CATEGORIES: '分类',
            TAGS: '标签',
            UNTITLED: '(未命名)',
        },
        ROOT_URL: '/blog/',
        CONTENT_URL: '/blog/content.json',
    };
    window.INSIGHT_CONFIG = INSIGHT_CONFIG;
})(window);
</script>
<script src="/blog/js/insight.js"></script>





   
<script async src="//busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script>





   






</body>
</html>